home *** CD-ROM | disk | FTP | other *** search
/ Resource for Source: C/C++ / Resource for Source - C-C++.iso / codelib3 / v_03_03 / fader.exe / DEMO / TESTFAD.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-11-18  |  17.1 KB  |  374 lines

  1. /* KNB Version 3.10 */
  2. /*
  3.      File name:    TESTFAD.C
  4.      Description:  This source file demonstrates the use of
  5.      the Fader custom control in application programs.  The
  6.      base source code for this module was first generated
  7.      using the CASE:W code generator in verbose mode.
  8.      Sequence:     This is Listing #4 */
  9.  
  10. #define EXTERN
  11. #include "TESTFAD.h"   /* Generated by CASE:W */
  12. #include "fadbox.h"    /* Generated by SDK Dialog Box Editor */
  13. #include "fader.h"     /* Needed for fader custom control */
  14.  
  15. BOOL NEAR PASCAL RegisterControlClass (HANDLE hInstance);
  16.  
  17. /************************************************************************/
  18. /*                                                                      */
  19. /* Windows 3.0 Main Program Body                                        */
  20. /*                                                                      */
  21. /* The following routine is the Windows Main Program.  The Main Program */
  22. /* is executed when a program is selected from the Windows Control      */
  23. /* Panel or File Manager.  The WinMain routine registers and creates    */
  24. /* the program's main window and initializes global objects.  The       */
  25. /* WinMain routine also includes the applications message dispatch      */
  26. /* loop.  Every window message destined for the main window or any      */
  27. /* subordinate windows is obtained, possibly translated, and            */
  28. /* dispatched to a window or dialog processing function. The dispatch   */
  29. /* loop is exited when a WM_QUIT message is obtained.  Before exiting   */
  30. /* the WinMain routine should destroy any objects created and free      */
  31. /* memory and other resources.                                          */
  32. /*                                                                      */
  33. /************************************************************************/
  34.  
  35. int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow)
  36. {
  37.  /***********************************************************************/
  38.  /* HANDLE hInstance;       handle for this instance                    */
  39.  /* HANDLE hPrevInstance;   handle for possible previous instances      */
  40.  /* LPSTR  lpszCmdLine;     long pointer to exec command line           */
  41.  /* int    nCmdShow;        Show code for main window display           */
  42.  /***********************************************************************/
  43.  
  44.  MSG      msg;           /* MSG structure to store your messages        */
  45.  int      nRc;           /* return value from Register Classes          */
  46.  HANDLE   hLibFader;
  47.  
  48.  strcpy(szarAppName, "TESTFAD");
  49.  hInst = hInstance;
  50.  if(!hPrevInstance)
  51.    {
  52.     /* register window classes if first instance of application         */
  53.     if ((nRc = nCwRegisterClasses()) == -1)
  54.       {
  55.        /* registering one of the windows failed                         */
  56.        LoadString(hInst, IDS_ERR_REGISTER_CLASS, szarString, sizeof(szarString));
  57.        MessageBox(NULL, szarString, NULL, MB_ICONEXCLAMATION);
  58.        return nRc;
  59.       }
  60.    }
  61.  
  62.  
  63.  /* create application's Main window                                    */
  64.  hWndMain = CreateWindow(
  65.                 szarAppName,             /* Window class name           */
  66.                 NULL,                    /* no title                    */
  67.                 WS_CAPTION      |        /* Title and Min/Max           */
  68.                 WS_SYSMENU      |        /* Add system menu box         */
  69.                 WS_MINIMIZEBOX  |        /* Add minimize box            */
  70.                 WS_MAXIMIZEBOX  |        /* Add maximize box            */
  71.                 WS_THICKFRAME   |        /* thick sizeable frame        */
  72.                 WS_CLIPCHILDREN |        /* don't draw in child windows areas */
  73.                 WS_VISIBLE      |        /* window created visible      */
  74.                 WS_OVERLAPPED,
  75.                 CW_USEDEFAULT, 0,        /* Use default X, Y            */
  76.                 CW_USEDEFAULT, 0,        /* Use default X, Y            */
  77.                 NULL,                    /* Parent window's handle      */
  78.                 NULL,                    /* Default to Class Menu       */
  79.                 hInst,                   /* Instance of window          */
  80.                 NULL);                   /* Create struct for WM_CREATE */
  81.  
  82.  
  83.  
  84.  if(hWndMain == NULL)
  85.    {
  86.     LoadString(hInst, IDS_ERR_CREATE_WINDOW, szarString, sizeof(szarString));
  87.     MessageBox(NULL, szarString, NULL, MB_ICONEXCLAMATION);
  88.     return IDS_ERR_CREATE_WINDOW;
  89.    }
  90.  
  91.  
  92.  ShowWindow(hWndMain, nCmdShow);            /* display main window      */
  93.  hLibFader = LoadLibrary("FADER.DLL");
  94.  if (hLibFader < 32)
  95.    return(0);
  96.  
  97.  while(GetMessage(&msg, NULL, 0, 0))        /* Until WM_QUIT message    */
  98.    {
  99.     TranslateMessage(&msg);
  100.     DispatchMessage(&msg);
  101.    }
  102.  
  103.  /* Do clean up before exiting from the application                     */
  104.  CwUnRegisterClasses();
  105.  FreeLibrary(hLibFader);
  106.  return msg.wParam;
  107. } /*  End of WinMain  */
  108.  
  109.  
  110.  
  111. /************************************************************************/
  112. /*                                                                      */
  113. /* Main Window Procedure                                                */
  114. /*                                                                      */
  115. /* This procedure provides service routines for the Windows events      */
  116. /* (messages) that Windows sends to the window, as well as the user     */
  117. /* initiated events (messages) that are generated when the user selects */
  118. /* the action bar and pulldown menu controls or the corresponding       */
  119. /* keyboard accelerators.                                               */
  120. /*                                                                      */
  121. /* The SWITCH statement shown below distributes the window messages to  */
  122. /* the respective message service routines, which are set apart by the  */
  123. /* CASE statements. The window procedures must provide an appropriate   */
  124. /* service routine for its end user initiated messages, as well as the  */
  125. /* general Windows messages (ie. WM_CLOSE message). If a message is     */
  126. /* sent to this procedure for which there is no programmed CASE clause  */
  127. /* (i.e., no service routine), the message is defaulted to the          */
  128. /* DefWindowProc function, where it is handled by Windows               */
  129. /*                                                                      */
  130. /************************************************************************/
  131.  
  132. LONG FAR PASCAL WndProc(HWND hWnd, WORD Message, WORD wParam, LONG lParam)
  133. {
  134.  HMENU       hMenu=0;            /* handle for the menu                 */
  135.  HBITMAP     hBitmap=0;          /* handle for bitmaps                  */
  136.  HDC         hDC;                /* handle for the display device       */
  137.  HANDLE      hPen=0;             /* handle for the current pen          */
  138.  PAINTSTRUCT ps;                 /* holds PAINT information             */
  139.  int         nRc=0;               /* return code                         */
  140.  
  141.  switch (Message)
  142.    {
  143.     case WM_COMMAND:
  144.          /* The Windows messages for action bar and pulldown menu items */
  145.          /* are processed here.                                         */
  146.          switch (wParam)
  147.            {
  148.             case IDM_D_FADERS:
  149.                  /* Place User Code to respond to the                   */
  150.                  /* Menu Item Named "&Faders..." here.                  */
  151.                  {
  152.                   FARPROC lpfnFADBOXMsgProc;
  153.  
  154.                   lpfnFADBOXMsgProc = MakeProcInstance((FARPROC)FADBOXMsgProc, hInst);
  155.                   nRc = DialogBox(hInst, (LPSTR)"FADBOX", hWnd, lpfnFADBOXMsgProc);
  156.                   FreeProcInstance(lpfnFADBOXMsgProc);
  157.                  }
  158.                  break;
  159.  
  160.             case IDM_D_EXIT:
  161.                  /* Place User Code to respond to the                   */
  162.                  /* Menu Item Named "E&xit" here.                       */
  163.                  DestroyWindow(hWnd);
  164.                  if (hWnd == hWndMain)
  165.                    PostQuitMessage(0);  /* Quit the application                 */
  166.                  hWndMain = 0;                     /* Clear the handle          */
  167.                  break;
  168.  
  169.             default:
  170.                 return DefWindowProc(hWnd, Message, wParam, lParam);
  171.                 break;
  172.            }
  173.          break;        /* End of WM_COMMAND                             */
  174.  
  175.     case WM_CREATE:
  176.          /* The WM_CREATE message is sent once to a window when the     */
  177.          /* window is created.  The window procedure for the new window */
  178.          /* receives this message after the window is created, but      */
  179.          /* before the window becomes visible.                          */
  180.  
  181.  
  182.          break;       /*  End of WM_CREATE                              */
  183.  
  184.     case WM_MOVE:     /*  code for moving the window                    */
  185.          break;
  186.  
  187.     case WM_SIZE:     /*  code for sizing client area                   */
  188.          break;       /* End of WM_SIZE                                 */
  189.  
  190.  
  191.     case WM_PAINT:    /* code for the window's client area              */
  192.          /* Obtain a handle to the device context                       */
  193.          /* BeginPaint will sends WM_ERASEBKGND if appropriate          */
  194.          memset(&ps, 0x00, sizeof(PAINTSTRUCT));
  195.          hDC = BeginPaint(hWnd, &ps);
  196.  
  197.          /* Inform Windows painting is complete                         */
  198.          EndPaint(hWnd, &ps);
  199.          break;       /*  End of WM_PAINT                               */
  200.  
  201.     case WM_CLOSE:  /* close the window                                 */
  202.          /* Destroy child windows, modeless dialogs, then, this window  */
  203.          DestroyWindow(hWnd);
  204.          if (hWnd == hWndMain)
  205.            PostQuitMessage(0);  /* Quit the application                 */
  206.          hWndMain = 0;                     /* Clear the handle          */
  207.          break;                 /* End of WM_CLOSE                      */
  208.  
  209.  
  210.     default:
  211.          /* For any message for which you don't specifically provide a  */
  212.          /* service routine, you should return the message to Windows   */
  213.          /* for default message processing.                             */
  214.          return DefWindowProc(hWnd, Message, wParam, lParam);
  215.          break;     /* End of default                                   */
  216.    }
  217.  return 0L;
  218. }     /* End of WndProc                                         */
  219. /************************************************************************/
  220. /*                                                                      */
  221. /* Dialog Window Procedure                                              */
  222. /*                                                                      */
  223. /* This procedure is associated with the dialog box that is included in */
  224. /* the function name of the procedure. It provides the service routines */
  225. /* for the events (messages) that occur because the end user operates   */
  226. /* one of the dialog box's buttons, entry fields, or controls.          */
  227. /*                                                                      */
  228. /* The SWITCH statement in the function distributes the dialog box      */
  229. /* messages to the respective service routines, which are set apart by  */
  230. /* the CASE clauses. Like any other Windows window, the Dialog Window   */
  231. /* procedures must provide an appropriate service routine for their end */
  232. /* user initiated messages as well as for general messages (like the    */
  233. /* WM_CLOSE message).                                                   */
  234. /* Dialog messages are processed internally by windows and passed to the*/
  235. /* Dialog Message Procedure. IF processing is done for a Message the    */
  236. /* Message procedure returns a TRUE, else , for messages not explicitly */
  237. /* processed, it returns a FALSE                                        */
  238. /*                                                                      */
  239. /************************************************************************/
  240.  
  241. BOOL FAR PASCAL FADBOXMsgProc(HWND hWndDlg, WORD Message, WORD wParam, LONG lParam)
  242. {
  243.  int    nRc;                         /*  Return code from Dialog boxes  */
  244.  int    nJ ;                         /*  Counter                        */
  245.  HANDLE hCtl;                        /*  Handle to dialog controls      */
  246.  static char szarWorkBuf[64];        /*  Work buffer for processing     */
  247.  static NPSTR lpszEndPtr;  /*  Temporary pointer.             */
  248.  char text_val[40];
  249.  int x;
  250.  BOOL lTranslated;
  251.  
  252.  switch(Message)
  253.    {
  254.     case WM_INITDIALOG:
  255.          /* initialize working variables                                */
  256.          nRc = 0;
  257.          hCtl = 0;
  258.          nJ = 0;
  259.          strcpy(szarWorkBuf, "");
  260.          lpszEndPtr = 0;
  261.  
  262.          SetDlgItemText(hWndDlg, FADER_EDIT, (LPSTR)"TESTING");
  263.  
  264.          break; /* End of WM_INITDLG                                    */
  265.  
  266.     case WM_CLOSE:
  267.          /* Closing the Dialog behaves the same as Cancel               */
  268.          /* PostMessage(hWndDlg, WM_COMMAND, IDCANCEL, 0L); */
  269.          EndDialog(hWndDlg, TRUE);
  270.          break; /* End of WM_CLOSE                                      */
  271.  
  272.     case WM_COMMAND:
  273.          switch(wParam)
  274.            {
  275.             case FADER_OK:
  276.                  /* Ignore data values entered into the controls        */
  277.                  /* and dismiss the dialog window returning FALSE       */
  278.                  EndDialog(hWndDlg, FALSE);
  279.                  break;
  280.  
  281.             case FADER_1:
  282.                  switch (HIWORD(lParam)) {
  283.                     case FDRN_THUMBTRACK:
  284.                     case FDRN_ENDFADER:
  285.                        /* User has changed the current value of the Fader */
  286.                        /* Request the current value from the Fader */
  287.                        x = (int) SendMessage(LOWORD(lParam), FDRM_GETLOGVALUE, 0, 0);
  288.  
  289.                        wsprintf(text_val,"%d",x);
  290.                        SetDlgItemText(hWndDlg, FADER_EDIT, (LPSTR)text_val);
  291.                        break;
  292.                      }
  293.                  break;
  294.  
  295.             case FADER_EDIT:
  296.                  /* If the value in the edit box changes, we will set the
  297.                     fader to use this as the new logical position */
  298.                  if (HIWORD(lParam) == EN_CHANGE) {
  299.                      x = GetDlgItemInt(hWndDlg, FADER_EDIT, (BOOL FAR *)&lTranslated, FALSE);
  300.                      SendDlgItemMessage(hWndDlg, FADER_1, FDRM_SETLOGVALUE, x, 0);
  301.                      }
  302.                  break;
  303.            }
  304.          break;    /* End of WM_COMMAND                                 */
  305.  
  306.     default:
  307.         return FALSE;
  308.    }
  309.  return TRUE;
  310. } /* End of FADBOXMsgProc                                      */
  311.  
  312.  
  313. /************************************************************************/
  314. /*                                                                      */
  315. /* nCwRegisterClasses Function                                             */
  316. /*                                                                      */
  317. /* The following function registers all the classes of all the windows  */
  318. /* associated with this application. The function returns an error code */
  319. /* if unsuccessful, otherwise it returns 0.                             */
  320. /*                                                                      */
  321. /************************************************************************/
  322.  
  323. int nCwRegisterClasses(void)
  324. {
  325.  WNDCLASS  wndclass;    /* struct to define a window class              */
  326.  
  327.  
  328.  /* load WNDCLASS with window's characteristics                         */
  329.  wndclass.style = CS_HREDRAW | CS_VREDRAW;
  330.  
  331.  wndclass.lpfnWndProc = WndProc;
  332.  
  333.  /* Extra storage for Class and Window objects                          */
  334.  wndclass.cbClsExtra = 0;
  335.  wndclass.cbWndExtra = 0;
  336.  wndclass.hInstance = hInst;
  337.  wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  338.  wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  339.  
  340.  /* Create brush for erasing background                                 */
  341.  wndclass.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  342.  wndclass.lpszMenuName = szarAppName;    /* Class Name is Menu Name     */
  343.  wndclass.lpszClassName = szarAppName;  /* Class Name is Window Name      */
  344.  
  345.  if(!RegisterClass(&wndclass))
  346.    return -1;
  347.  
  348.  return 0;
  349. }  /* End of nCwRegisterClasses */
  350.  
  351.  
  352.  
  353. /************************************************************************/
  354. /*  CwUnRegisterClasses Function                                          */
  355. /*                                                                      */
  356. /*  Deletes any refrences to windows resources created for this         */
  357. /*  application, frees memory, deletes instance, handles and does       */
  358. /*  clean up prior to exiting the window                                */
  359. /*                                                                      */
  360. /************************************************************************/
  361.  
  362. void CwUnRegisterClasses(void)
  363. {
  364.  WNDCLASS  wndclass;    /* struct to define a window class              */
  365.  memset(&wndclass, 0x00, sizeof(WNDCLASS));
  366.  
  367.  UnregisterClass(szarAppName, hInst);
  368.  
  369.  
  370. }           /* End of CwUnRegisterClasses                                 */
  371.  
  372.  
  373.  
  374.